package com.gateway.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Primary
import org.springframework.stereotype.Component
import org.springframework.web.reactive.config.ResourceHandlerRegistry
import org.springframework.web.reactive.config.WebFluxConfigurer
import springfox.documentation.swagger.web.SwaggerResource
import springfox.documentation.swagger.web.SwaggerResourcesProvider


//http://localhost:81/swagger-ui.html
//http://localhost:81/docs.html,http://localhost:81/gateway_???/docs.html

@Component
@Primary
class SwaggerConfig : SwaggerResourcesProvider {
    @Bean
    fun configurer(): WebFluxConfigurer {
        return object : WebFluxConfigurer {
            override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
                registry.addResourceHandler("/**").addResourceLocations("classpath:/static/")
                registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/")
                registry.addResourceHandler("docs.html").addResourceLocations("classpath:/META-INF/resources/")
                registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/")
            }
        }
    }

    override fun get(): List<SwaggerResource> {
        return listOf(
            swaggerRes("ApiDoc", "/gateway_api/v2/api-docs"),
            swaggerRes("Service1Doc", "/gateway_service1/v2/api-docs"),
            swaggerRes("Service2Doc", "/gateway_service2/v2/api-docs"),
            swaggerRes("SocketDoc", "/gateway_socket/v2/api-docs")
        )
    }

    fun swaggerRes(name: String, location: String): SwaggerResource {
        val res = SwaggerResource()
        res.name = name
        res.location = location
        res.swaggerVersion = "2.0"
        return res
    }
}
